package leetcode_acm.array;

import org.junit.Test;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * 功能描述
 *
 * @author: 张庭杰
 * @date: 2023年03月21日 20:26
 */
public class Intersect {
    public int[] intersect(int[] nums1, int[] nums2) {
        Arrays.sort(nums1);
        Arrays.sort(nums2);
        int p1 = 0, p2 = 0;
        int n = nums1.length;
        int m = nums2.length;
        List<Integer> list = new LinkedList<>();
        while(p1 < n && p2 < m){
            if(nums1[p1] == nums2[p2]){
                list.add(nums1[p1]);
                p1++;p2++;
            }else if(nums1[p1] > nums2[p2]){
                p2++;
            }else{
                p1++;
            }
        }
        int length = list.size();
        int[] ans = new int[length];
        for (int i = 0; i < ans.length; i++) {
            ans[i] = list.get(i);
        }
        return ans;
    }

    @Test
    public void test(){
        System.out.println(Arrays.toString(intersect(new int[]{1, 2, 2, 1}, new int[]{2, 2})));
    }
}
